﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAnDAL;

namespace DoAn
{
    public partial class SinhVien : System.Web.UI.Page
    {
        private string _connectionString = ConfigurationManager.ConnectionStrings["HOUSMSConnectionString"].ConnectionString;
        HousmsDataContext _housmsDataContext = new HousmsDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
           
            var listKy = _housmsDataContext.tblKies.Select(k => new {k.Ky, k.TenKy});
            ddlKy.DataValueField = "Ky";
            ddlKy.DataTextField = "TenKy";
            ddlKy.DataSource = listKy;
            if (IsPostBack == false)
            {
                ddlKy.DataBind();
            }
            


           

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {

        }

        protected void ddlKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedKy = int.Parse(ddlKy.SelectedValue);
            var listLop = _housmsDataContext.tblLops.Select(l =>l).Where(l=>(l.Ky == selectedKy));
            ddlLop.DataTextField = "TenLop";
            ddlLop.DataValueField = "TenLop";
            ddlLop.DataSource = listLop;
            ddlLop.DataBind();


            var listMon = _housmsDataContext.tblMonHocs.Select(m => new { m.MaMonHoc, m.TenMonHoc, m.Ky }).Where(m => (m.Ky == selectedKy)).Distinct().ToList();
            ddlMonhoc.DataValueField = "MaMonHoc";
            ddlMonhoc.DataTextField = "TenMonHoc";
            ddlMonhoc.DataSource = listMon;
            ddlMonhoc.DataBind();
        }

        protected void ddlLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedLop = ddlLop.SelectedValue.Trim();
            var listKetQua =
                _housmsDataContext.tblKetQuas.Select(kq=>new
                                                             {
                                                                 kq.tblSinhVien.TenSV,
                                                                 kq.tblSinhVien.NgaySinh,
                                                                 kq.LanThi,
                                                                 kq.DiemThi,
                                                                 kq.DiemThanhPhan, 
                                                                 kq.DiemTongKet, 
                                                                 kq.TenLop, 
                                                                 kq.tblMonHoc.TenMonHoc, 
                                                                 Trangthai = (kq.DiemThi>50)? "Đạt":"Không Đạt"
                                                             }
                                                         ).Where(kq => (kq.TenLop == selectedLop)).ToList();

        
            dgvKetqua.DataSource = listKetQua;
            dgvKetqua.DataBind();

           
        }

        protected void ddlMonhoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 selectedMonHoc = Int32.Parse(ddlMonhoc.SelectedValue.Trim());
            string selectedLop = ddlLop.SelectedValue.Trim();
            var listKetQua =
                _housmsDataContext.tblKetQuas.Select(kq => new
                {
                    kq.tblSinhVien.TenSV,
                    kq.tblSinhVien.NgaySinh,
                    kq.LanThi,
                    kq.DiemThi,
                    kq.DiemThanhPhan,
                    kq.DiemTongKet,
                    kq.TenLop,
                    kq.tblMonHoc.TenMonHoc,
                    kq.MaMonHoc,
                    Trangthai = (kq.DiemThi > 50) ? "Đạt" : "Không Đạt"
                }
                                                         ).Where(kq => (kq.TenLop == selectedLop && kq.MaMonHoc== selectedMonHoc)).ToList();


            dgvKetqua.DataSource = listKetQua;
            dgvKetqua.DataBind();
        }
    }
}